﻿using System;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Players
{
    public partial class Manager
    {
        private const int CameraOffset = 32;

        private Container _camera;
        private Timer _cameraTimer;
        private ITrajectory _cameraEasing;
        private TE_Vector2 _cameraStartPosition, _cameraEndPosition;

        void InitializeCamera()
        {
            _camera = new Container();
            _cameraTimer = new Timer(300, false, EndCameraMovement);
            _cameraEasing = new QuadraticEasing(BaseTrajectory.Easing.EaseOut);

            AddChildren(_camera);
            SetCameraPosition(_player.Position);
        }

        void UpdateCamera(TimeSpan gameTime)
        {
            if(_cameraTimer.IsStarted)
                _camera.Position = (_cameraEndPosition - _cameraStartPosition) *
                                   _cameraEasing.GetPercentageProgression(_cameraTimer.PercentageElapsed) +
                                   _cameraStartPosition;
        }

        void EndCameraMovement()
        {
            _camera.Position = _cameraEndPosition;
        }

        void CalculateCameraPosition(bool hasWarped)
        {
            var newPosition = new TE_Vector2(_player.Position);

            
            switch (_latestDirection)
            {
                case Direction.Down:
                    newPosition.AddOffset(0, CameraOffset);
                    break;
                case Direction.Up:
                    newPosition.AddOffset(0, -CameraOffset);
                    break;
                case Direction.Left:
                    newPosition.AddOffset(-CameraOffset, 0);
                    break;
                case Direction.Right:
                    newPosition.AddOffset(CameraOffset, 0);
                    break;
            }
            newPosition = ClampCamera(newPosition);

            if (hasWarped)
            {
                SetCameraPosition(newPosition);
            }
            else
            {
                _cameraStartPosition = _camera.Position;
                _cameraEndPosition = newPosition;
                _cameraTimer.Reset();
            }

            
        }
        void SetCameraPosition(TE_Vector2 newPosition)
        {
            _camera.Position = ClampCamera(newPosition);

            _cameraTimer.Stop();
        }

        TE_Vector2 ClampCamera(TE_Vector2 newPosition)
        {
            var mapSize = Playscreen.Map.Size;
            var viewSize = Playscreen.HalfViewSize;

            return new TE_Vector2(newPosition.X.Clamp(viewSize.X, mapSize.X - viewSize.X - 32), newPosition.Y.Clamp(viewSize.Y, mapSize.Y - viewSize.Y + 32));
        }

        public TE_Vector2 CameraPosition
        {
            get
            {
                return _camera.Position;
            }
        }
    }
}